<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/token.jsp"%>
<%
    Integer uid = token.getUid();
    List<UserProfile> userProfiles = QBuilder.db("basedb").select().from("UserProfile").where(new Where("hospital=", hospital).and("status=", STATUS_ENABLE).and("uid=", uid)).query().getResultObj();
    String uids = "" + uid;
    for (int i = 0; i < userProfiles.size(); i++) {
        UserProfile userProfile = userProfiles.get(i);
        uids += "," + userProfile.getId();
    }
    List<Map> orders = QBuilder.db("basedb").select("new map(id as register_id, patientName as patient_name, doctorName as doctor_name, deptName as dept_name, date as date, half as half, registerStatus as register_status, branch as branch, jsonString as json)").from("RegisterOrder").where(new Where("hospital=", hospital).and("status=", STATUS_ENABLE).and("userProfileId in("+ uids +")" )).order("date", QBuilder.desc).query().getResultObj();
    if (isEmpty(orders)) {
        msg = new Message(30041, "暂无预约信息");
        msg.put("data", new ArrayList());
        Tools.encryptPrint(out, msg);
        return;
    }
	Collections.sort(orders, new Comparator<Map>() {
		public int compare(Map o1, Map o2) {
			 String json = Tools.getDesc(o1.get("json"), "");
			Map<String, String> _map1 = JSONUtil.fromJson(json, new TypeReference<Map<String, String>>(){});
			json = Tools.getDesc(o2.get("json"), "");
			Map<String, String> _map2 = JSONUtil.fromJson(json, new TypeReference<Map<String, String>>(){});
			return Tools.getDesc(o2.get("date") + _map2.get("start_time")).compareTo(Tools.getDesc(o1.get("date")+ _map1.get("start_time")));
		}
	});
    Map<String, List<Map>> tempMap = new HashMap<String, List<Map>>();
    tempMap.put("本部", new ArrayList());
    tempMap.put("东区", new ArrayList());
    tempMap.put("北区", new ArrayList());
    for (int i = 0; i < orders.size(); i++) {
        Map map = orders.get(i);
        String branch = Tools.getDesc(map.get("branch"), "1");
        String branchName = "1".equals(branch) ? "本部" : "2".equals(branch) ? "东区" : "北区";
        String json = Tools.getDesc(map.remove("json"), "");
        Map<String, String> _map = JSONUtil.fromJson(json, new TypeReference<Map<String, String>>(){});
        map.put("start_time", _map.get("start_time"));
        map.put("end_time", _map.get("end_time"));
        map.put("branch_name", branchName);
		String registerDateStr = Tools.getDesc(map.get("date")) + " " + Tools.getDesc(_map.get("start_time"));
		Date registerDate = new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(registerDateStr);
		if (registerDate.before(new Date())) {
			Integer registerStatus = (Integer)map.get("register_status");
			if (registerStatus == 1) {
				map.put("register_status", 2);
			}
		}	
        List<Map> tempList = tempMap.get(branchName);
        if (tempList == null) {
            tempList = new ArrayList();
            tempMap.put(branchName, tempList);
        }
        tempList.add(map);
    }
    Set<Entry<String, List<Map>>> set = tempMap.entrySet();
    List<Map> result = new ArrayList<Map>();
    for (Entry<String, List<Map>> entry : set) {
        String key = entry.getKey();
        List<Map> values = entry.getValue();
        List<Map> exttr = new ArrayList<>();
        Iterator<Map> it = values.iterator();
        while(it.hasNext()){
            Map x = it.next();
            Integer registerStatus = (Integer)x.get("register_status");
            if(registerStatus == 0){
                exttr.add(x);
                it.remove();
            }
        }
        values.addAll(exttr);
        Map map = new HashMap();
        map.put("branch_name", key);
        map.put("result", values);
        result.add(map);
    }
    msg.put("data", result);
    Tools.encryptPrint(out, msg);
%>
